﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HR_ASP.ViewModel;
using DB.Model;

namespace HR_ASP.Controllers
{
    public class StanowiskoController : Controller
    {
        //
        // GET: /Stanowisko/

        public ActionResult Index()
        {

            using (var context = new HREntities())
            {
                return View(context.Stanowiska.Select(s => new StanowiskoViewModel
                {
                    ID = s.ID_Stan,
                    Nazwa = s.Nazwa,
                    Opis = s.Opis
                }).ToArray()
                );                
            }
        }

        public ActionResult Create()
        {
            return View("Create");
        }

        [HttpPost]
        public ActionResult Create(StanowiskoViewModel st)
        {
            using (var context = new HREntities())
            {              
                context.Stanowiska.AddObject(st.GetModel(new Stanowisko()));
                context.SaveChanges();
            }
            return RedirectToAction("Index","DzialStanow",st);
        }

        public ActionResult Edit(int id)
        {
            using (var context = new HREntities())
            {
                StanowiskoViewModel st = new StanowiskoViewModel(context.Stanowiska.SingleOrDefault(s => s.ID_Stan == id));
                if (st == null)
                {
                    return RedirectToAction("Index");
                }             
                    var tmpDzialy= context.Dzialy.Select(n=> new DzialViewModel(){
                        ID = n.ID_Dzial,
                        Nazwa = n.Nazwa
                }).ToArray();

                    foreach (var item in tmpDzialy)
                    {
                      
                        foreach (var item2 in st.Dzialy)
                        {
                            if (item.ID == item2.ID)
                            {
                                item.CzyZaznaczone = true;
                            }
                        }//TODO
                    }
                   // st.Dzialy = tmpDzialy;
                return View(st);
            }
        }

        [HttpPost]
        public ActionResult Edit(StanowiskoViewModel st)
        {
            using (var context = new HREntities())
            {
                var stanowiskoDB = context.Stanowiska.SingleOrDefault(s => s.ID_Stan == st.ID);
                context.ApplyCurrentValues<Stanowisko>("Stanowiska", st.GetModel(stanowiskoDB));
                context.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            using (var context = new HREntities())
            {
                var employee = context.Stanowiska.SingleOrDefault(s => s.ID_Stan == id);
                if (employee != null)
                {
                    context.Stanowiska.DeleteObject(employee);
                    context.SaveChanges();
                }
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Delete(StanowiskoViewModel[] stanowiska)
        {
            using (var context = new HREntities())
            {
                foreach (var item in stanowiska)
                {
                    if (item.CzyZaznaczone)
                    {
                        var employee = context.Stanowiska.SingleOrDefault(s => s.ID_Stan == item.ID);
                        if (employee != null)
                        {
                            context.Stanowiska.DeleteObject(employee);                            
                        }
                    }
                }
                context.SaveChanges();              
            }
            return RedirectToAction("Index");
        }

        public ActionResult Details(int id)
        {
            using (var context = new HREntities())
            {
               StanowiskoViewModel stanowisko =  new StanowiskoViewModel(context.Stanowiska.SingleOrDefault(s => s.ID_Stan == id));
                if (stanowisko == null)
                {
                    return RedirectToAction("Error");
                }                
                return View(stanowisko);
            }
        }

    }
}
